//
//  WaittingVC.m
//  MovieDate
//
//  Created by yyta on 16/4/1.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "WaittingVC.h"
#import "WaittingCell.h"
#import "DateDetailVC.h"
@interface WaittingVC ()<UITableViewDataSource,UITableViewDelegate,waittingCellDelegate,UIAlertViewDelegate>
{
    int pIntPage;
    
    //取消影约记录字段
    NSString *pCurappId;
    NSString *pCurRelease;
    
}
@property(nonatomic, strong) NSMutableArray *pDataDicArr;

@property (weak, nonatomic) IBOutlet UILabel *lblMessage;

@end

@implementation WaittingVC
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;

}
- (void)initInfo
{
    [super initInfo];
    pIntPage = 1;
    _pDataDicArr = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self selAddLeftBackButton];
    [self selDefaultNavigationBackground];
    [self selDefaultNavigationWithTitle:@"等待确认"];
    
    [self.pTableView setTableFooterView:[UIView new]];
    
    __weak typeof(self) weakSelf = self;
    self.pTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        pIntPage = 1;
        [weakSelf requestDataDic];
    }];
    
    [self.pTableView.mj_header beginRefreshing];
    
    self.pTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestDataDic];
    }];
    
    [self.pTableView.mj_footer setHidden:YES];
}

- (void)requestDataDic
{
       __weak typeof(self) weakSelf = self;
   [TDNetwork requestMyConfirmMovieList:MY_Confirm_MovieList page:pIntPage pageSize:10 complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
       if (succeed) {
           [weakSelf.pDataDicArr removeAllObjects];
       
           NSDictionary *dict = [dictionary valueForKey:@"res"];
           if (!dict || [dict allKeys].count == 0) {
               return;
           }
           NSArray *arr = [dict valueForKey:@"movieList"];
           [_pDataDicArr addObjectsFromArray:arr];
       
           [weakSelf.pTableView reloadData];
           
           if (arr.count == 10) {
               pIntPage ++;
               [self.pTableView.mj_footer setHidden:NO];
           } else {
               [self.pTableView.mj_footer setHidden:YES];
           }
       } else {
       }
       
       if (_pDataDicArr.count == 0) {
           [_lblMessage setHidden:NO];
       } else {
           [_lblMessage setHidden:YES];
       }
       
       [self.pTableView.mj_footer endRefreshing];
       [self.pTableView.mj_header endRefreshing];
 
    
   }];
}
#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _pDataDicArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WaittingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"idfWaittingCell"];
    if (!cell) {
        cell = [[WaittingCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"idfWaittingCell"];
    }

    UIImageView *pimageView =  [cell viewWithTag:1301];
    UIImageView *sexImageView = [cell viewWithTag:1302];
    UILabel *nameLB = [cell viewWithTag:1101];
    UILabel *ageLB = [cell viewWithTag:1102];
    UILabel *timeLB = [cell viewWithTag:1103];
    UILabel *addressLB = [cell viewWithTag:1104];
    NSDictionary *dic = [_pDataDicArr objectAtIndex:indexPath.row];
    cell.dataDic = dic;
    [pimageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", IP, [dic objectForKey:@"headImage"]]]];
    [sexImageView setImage:[UIImage imageNamed:[[dic objectForKey:@"userSex"] intValue]==0?@"sex_girl":@"sex_boy"]];
    nameLB.text = [dic valueForKey:@"nickname"];
    ageLB.text = [dic valueForKey:@"userAge"];
    timeLB.text = [dic valueForKey:@"releaseTime"];
    addressLB.text = [dic valueForKey:@"meetAddress"];
    cell.delegate = self;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIStoryboard *logStoyBoard = [UIStoryboard storyboardWithName:@"See" bundle:nil];
    
    DateDetailVC  *dateDetailVC = [logStoyBoard instantiateViewControllerWithIdentifier:@"idDateDetailVC"];
    
    dateDetailVC.pDict = [NSMutableDictionary dictionaryWithDictionary:[_pDataDicArr objectAtIndex:indexPath.row]];
    [dateDetailVC.pDict setValue:@"2" forKey:@"btnHidden"];
    
    [self.navigationController pushViewController:dateDetailVC animated:YES];
}

-(void)pushNextVC:(NSDictionary *)dic{
    UIStoryboard *logStoyBoard = [UIStoryboard storyboardWithName:@"See" bundle:nil];

    DateDetailVC  *dateDetailVC = [logStoyBoard instantiateViewControllerWithIdentifier:@"idDateDetailVC"];
    dateDetailVC.pDict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [dateDetailVC.pDict setValue:@"2" forKey:@"btnHidden"];
    
    [self.navigationController pushViewController:dateDetailVC animated:YES];
    
}

-(void)cancleWaittingAction:(NSString *)appid andReleaseTime:(NSString *)releaseTime {
    pCurappId = appid;
    pCurRelease = releaseTime;
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"系统提示" message:@"是否取消等待" delegate:self cancelButtonTitle:@"点错了" otherButtonTitles:@"取消等待", nil];
    [alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        [TDNetwork  cancleAcceptMovieURl:cancelAccept_MovieDate appId:pCurappId releaseTime:pCurRelease  complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
            if (succeed) {
                [self.pTableView.mj_header beginRefreshing];
            }else{
            
            }
        }];
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
